$(function () {
    $(".loadingMarsk").css({
        "width":window.screen.width,
        "height":window.screen.height,
    });
    var token = localStorage.token;
    if (!token || !localStorage.sureOrderProductDetail) {
        location.href = "../loginAndReg/login.html";
        return false;
    }
    new FastClick(document.body);
    var plist = JSON.parse(localStorage.sureOrderProductDetail);
    var totalDiamond = 0;
    var totalCash = 0;
    var data = "";
    $.each(plist, function (index, value) {
        totalDiamond += parseFloat(value.diamond) * parseInt(value.count);
        totalCash += parseFloat(value.cash) * parseInt(value.count);
        data += value.skuId + ":" + value.count + ";";
    });
    data = data.substr(0, data.length - 1);
    //选择地址渲染
    var choseAddressStatus = parseInt(localStorage.choseAddressStatus);
    $.ajax({
        type: "get",
        url: domain.test + "/carcalculate",
        // url: domain.online + "/cart/confirm",
        beforeSend: function (xhr) {
            $(".loadingMarsk").css("display","block");
            xhr.setRequestHeader("token", token);
        },
        success: function (response) {
            console.log(response);
            if (response.data.rtnCode === 200) {
                // if (response.rtnCode === 200) {
                var sureOrderDetail = response.data.data;
                // var sureOrderDetail = response.data;
                sureOrderDetail.pList = plist;
                sureOrderDetail.totalPrice = parseFloat(localStorage.totalPrice);
                sureOrderDetail.totalDiamond = parseFloat(totalDiamond);
                sureOrderDetail.totalCash = parseFloat(totalCash);
                if (choseAddressStatus === 1) {
                    var catchAdress = JSON.parse(localStorage.catchAdress);
                    sureOrderDetail.address.id = catchAdress.addressId;
                    sureOrderDetail.address.contact = catchAdress.contact;
                    sureOrderDetail.address.mobile = catchAdress.mobile;
                    sureOrderDetail.address.address = catchAdress.address;
                    localStorage.choseAddressStatus = "";
                }
                $(".orderDetail").html(template("orderDetail", { sureOrderDetail: sureOrderDetail }));
                $(".loadingMarsk").css("display","none");
            }
            if (response.data.rtnCode === 401) {
                // if (response.rtnCode === 401) {
                var outTimeCode = response.data.rtnCode;
                // var outTimeCode = response.rtnCode;
                location.href = "../../index.html?outTimeCode=" + outTimeCode;
            }
        }
    });
    //是否选择钻石加现金支付
    $(".orderDetail").on("click", ".diamondBtn", function () {
        if (parseInt($(this).attr("noMoreDiamond")) === 0) return false;
        var goldStatus = parseInt($(".goldBtn").attr("goldStatus"));
        var redPayStatus = parseInt($(".redPayBtn").attr("redPayStatus"));
        if (goldStatus === 0 && redPayStatus === 0) {
            if (parseInt($(this).attr("payType")) === 2) {
                $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                $(this).attr("payType", 1);
                $(".diamondPay").html("0.00");
                $(".wxPay").html(localStorage.totalPrice);
                $(".cashPay").html(localStorage.totalPrice);
            } else if (parseInt($(this).attr("payType")) === 1) {
                $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                $(this).attr("payType", 2);
                $(".diamondPay").html(totalDiamond.toFixed(2));
                $(".wxPay").html(totalCash.toFixed(2));
                $(".cashPay").html(totalCash.toFixed(2));
            }
        }
        if (goldStatus === 1 && redPayStatus === 0) {
            //取消钻石支付
            if (parseInt($(this).attr("payType")) === 2) {
                var coin = parseFloat($(".coinL").html());
                $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                $(this).attr("payType", 1);
                if (totalCash > 0) {
                    if (coin > parseFloat(localStorage.totalPrice)) {
                        $(".diamondPay").html("0.00");
                        $(".coinPayD").html(localStorage.totalPrice);
                    } else if (coin < parseFloat(localStorage.totalPrice)) {
                        if (coin - totalCash >= 0) {
                            $(".diamondPay").html("0.00");
                            $(".coinPayD").html(totalCash.toFixed(2));
                            $(".wxPay").html((parseFloat(localStorage.totalPrice) - totalCash).toFixed(2));
                            $(".cashPay").html((parseFloat(localStorage.totalPrice) - totalCash).toFixed(2));
                        } else if (coin - totalCash < 0) {
                            $(".diamondPay").html("0.00");
                            $(".coinPayD").html(coin.toFixed(2));
                            $(".wxPay").html((parseFloat(localStorage.totalPrice) - coin).toFixed(2));
                            $(".cashPay").html((parseFloat(localStorage.totalPrice) - coin).toFixed(2));
                        }
                    }
                } else if (totalCash === 0) {
                    if (coin >= parseFloat(localStorage.totalPrice)) {
                        $(".diamondPay").html("0.00");
                        $(".coinPayD").html(parseFloat(localStorage.totalPrice).toFixed(2));
                        $(".wxPay").html("0.00");
                        $(".cashPay").html("0.00");
                    } else if (coin < parseFloat(localStorage.totalPrice)) {
                        $(".diamondPay").html("0.00");
                        $(".coinPayD").html(coin.toFixed(2));
                        $(".wxPay").html((parseFloat(localStorage.totalPrice) - coin).toFixed(2));
                        $(".cashPay").html((parseFloat(localStorage.totalPrice) - coin).toFixed(2));
                    }
                }
            } else if (parseInt($(this).attr("payType")) === 1) {
                var coin = parseFloat($(".coinL").html());
                if (totalCash > 0) {
                    if (coin >= parseFloat(localStorage.totalPrice)) {
                        $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                        $(this).attr("payType", 2);
                        $(".diamondPay").html(totalDiamond.toFixed(2));
                        $(".coinPayD").html(totalCash.toFixed(2));
                        $(".wxPay").html("0.00");
                        $(".cashPay").html("0.00");
                    } else if (coin < parseFloat(localStorage.totalPrice)) {
                        $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                        $(this).attr("payType", 2);
                        $(".diamondPay").html(totalDiamond.toFixed(2));
                        $(".coinPayD").html(coin.toFixed(2));
                        $(".wxPay").html((totalCash - coin).toFixed(2));
                        $(".cashPay").html((totalCash - coin).toFixed(2));
                    }
                } else if (totalCash === 0) {
                    if (coin < parseFloat(localStorage.totalPrice)) {
                        $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                        $(this).attr("payType", 2);
                        $(".diamondPay").html(totalDiamond.toFixed(2));
                        $(".coinPayD").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".cashPay").html("0.00");
                    } else if (coin >= parseFloat(localStorage.totalPrice)) {
                        $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                        $(this).attr("payType", 2);
                        $(".diamondPay").html(totalDiamond.toFixed(2));
                        $(".coinPayD").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".cashPay").html("0.00");
                    }
                }
            }
        }
        if (goldStatus === 0 && redPayStatus === 1) {
            //取消钻石支付
            if (parseInt($(this).attr("payType")) === 2) {
                var red = parseFloat($(".redL").html());
                $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                $(this).attr("payType", 1);
                if (totalCash > 0) {
                    if (red > parseFloat(localStorage.totalPrice)) {
                        $(".diamondPay").html("0.00");
                        $(".redPayD").html(localStorage.totalPrice);
                    } else if (red < parseFloat(localStorage.totalPrice)) {
                        if (red - totalCash >= 0) {
                            $(".diamondPay").html("0.00");
                            $(".redPayD").html(totalCash.toFixed(2));
                            $(".wxPay").html((parseFloat(localStorage.totalPrice) - totalCash).toFixed(2));
                            $(".cashPay").html((parseFloat(localStorage.totalPrice) - totalCash).toFixed(2));
                        } else if (red - totalCash < 0) {
                            $(".diamondPay").html("0.00");
                            $(".redPayD").html(red.toFixed(2));
                            $(".wxPay").html((parseFloat(localStorage.totalPrice) - red).toFixed(2));
                            $(".cashPay").html((parseFloat(localStorage.totalPrice) - red).toFixed(2));
                        }
                    }
                } else if (totalCash === 0) {
                    if (red >= parseFloat(localStorage.totalPrice)) {
                        $(".diamondPay").html("0.00");
                        $(".redPayD").html(localStorage.totalPrice);
                        $(".wxPay").html("0.00");
                        $(".cashPay").html("0.00");
                    } else if (red < parseFloat(localStorage.totalPrice)) {
                        $(".diamondPay").html("0.00");
                        $(".redPayD").html(red.toFixed(2));
                        $(".wxPay").html((parseFloat(localStorage.totalPrice) - red).toFixed(2));
                        $(".cashPay").html((parseFloat(localStorage.totalPrice) - red).toFixed(2));
                    }
                }
            } else if (parseInt($(this).attr("payType")) === 1) {
                var red = parseFloat($(".redL").html());
                if (totalCash > 0) {
                    if (red < totalCash) {
                        $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                        $(this).attr("payType", 2);
                        $(".diamondPay").html(totalDiamond.toFixed(2));
                        $(".redPayD").html(red.toFixed(2));
                        $(".wxPay").html((totalCash - red).toFixed(2));
                        $(".cashPay").html((totalCash - red).toFixed(2));
                    } else if (red >= totalCash) {
                        $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                        $(this).attr("payType", 2);
                        $(".diamondPay").html(totalDiamond.toFixed(2));
                        $(".redPayD").html(totalCash.toFixed(2));
                        $(".wxPay").html("0.00");
                        $(".cashPay").html("0.00");
                    }
                } else if (totalCash === 0) {
                    if (red < parseFloat(localStorage.totalPrice)) {
                        $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                        $(this).attr("payType", 2);
                        $(".diamondPay").html(totalDiamond.toFixed(2));
                        $(".redPayD").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".cashPay").html("0.00");
                    } else if (red > parseFloat(localStorage.totalPrice)) {
                        $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                        $(this).attr("payType", 2);
                        $(".diamondPay").html(totalDiamond.toFixed(2));
                        $(".redPayD").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".cashPay").html("0.00");
                    }
                }
            }
        }
        if (goldStatus === 1 && redPayStatus === 1) {
            var coin = parseFloat($(".coinL").html());
            var red = parseFloat($(".redL").html());
            if (parseInt($(this).attr("payType")) === 2) {
                if ((coin + red) < totalCash) {
                    $(this).attr("payType", 1);
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(".diamondPay").html("0.00");
                    $(".cashPay").html((parseFloat(localStorage.totalPrice) - (coin + red)).toFixed(2));
                    $(".wxPay").html((parseFloat(localStorage.totalPrice) - (coin + red)).toFixed(2));
                }
                if (coin >= totalCash && coin < parseFloat(localStorage.totalPrice) && red > 0 && red < totalCash) {
                    $(this).attr("payType", 1);
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(".diamondPay").html("0.00");
                    $(".redPayD").html(red.toFixed(2));
                    $(".coinPayD").html(coin.toFixed(2));
                    $(".cashPay").html((parseFloat(localStorage.totalPrice) - (coin + red)).toFixed(2));
                    $(".wxPay").html((parseFloat(localStorage.totalPrice) - (coin + red)).toFixed(2));
                }
                if (coin > parseFloat(localStorage.totalPrice) && red > 0 && red < totalCash) {
                    $(this).attr("payType", 1);
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(".diamondPay").html("0.00");
                    $(".coinPayD").html((parseFloat(localStorage.totalPrice)).toFixed(2));
                    $(".redPayD").html("0.00");
                }
                if (red >= totalCash && red < (parseFloat(localStorage.totalPrice)) && coin > 0 && coin < totalCash) {
                    $(this).attr("payType", 1);
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(".diamondPay").html("0.00");
                    $(".redPayD").html(red.toFixed(2));
                    $(".coinPayD").html(coin.toFixed(2));
                    $(".cashPay").html((parseFloat(localStorage.totalPrice) - (coin + red)).toFixed(2));
                    $(".wxPay").html((parseFloat(localStorage.totalPrice) - (coin + red)).toFixed(2));
                }
                if (red > parseFloat(localStorage.totalPrice) && coin > 0 && coin < totalCash) {
                    $(this).attr("payType", 1);
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(".diamondPay").html("0.00");
                    $(".coinPayD").html(coin.toFixed(2));
                    $(".redPayD").html((parseFloat(localStorage.totalPrice) - coin).toFixed(2));
                }
                if (coin > totalCash && red > totalCash) {
                    $(this).attr("payType", 1);
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(".diamondPay").html("0.00");
                    $(".coinPayD").html((parseFloat(localStorage.totalPrice)).toFixed(2));
                    $(".redPayD").html("0.00");
                    $(".cashPay").html("0.00");
                    $(".wxPay").html("0.00");
                }
                if (coin > totalCash && red > totalCash && totalCash === 0) {
                    if (coin >= parseFloat(localStorage.totalPrice)) {
                        $(".coinPayD").html(parseFloat(localStorage.totalPrice));
                        $(".redPayD").html("0.00");
                        $(".cashPay").html("0.00");
                        $(".wxPay").html("0.00");
                    } else if (coin < parseFloat(localStorage.totalPrice)) {
                        $(".coinPayD").html(coin.toFixed(2));
                        if (red >= parseFloat(localStorage.totalPrice) - coin) {
                            $(".redPayD").html((parseFloat(localStorage.totalPrice) - coin).toFixed(2));
                            $(".cashPay").html("0.00");
                            $(".wxPay").html("0.00");
                        } else if (red < parseFloat(localStorage.totalPrice) - coin) {
                            $(".redPayD").html(red.toFixed(2));
                            $(".cashPay").html((parseFloat(localStorage.totalPrice) - coin - red).toFixed(2));
                            $(".wxPay").html((parseFloat(localStorage.totalPrice) - coin - red).toFixed(2));
                        }
                    }
                }
            } else if (parseInt($(this).attr("payType")) === 1) {
                if ((coin + red) < totalCash) {
                    $(this).attr("payType", 2);
                    $(".diamondPay").html(totalDiamond.toFixed(2));
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(".redPayD").html(red.toFixed(2));
                    $(".coinPayD").html(coin.toFixed(2));
                    $(".cashPay").html((totalCash - (coin + red)).toFixed(2));
                    $(".wxPay").html((totalCash - (coin + red)).toFixed(2));
                }
                if (coin >= totalCash && red > 0 && red < totalCash) {
                    $(this).attr("payType", 2);
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(".diamondPay").html(totalDiamond.toFixed(2));
                    $(".redPayD").html(red.toFixed(2));
                    $(".coinPayD").html(coin.toFixed(2));
                    $(".cashPay").html("0.00");
                    $(".wxPay").html("0.00");
                }
                if (red >= totalCash && coin > 0 && coin < totalCash) {
                    $(this).attr("payType", 2);
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(".diamondPay").html(totalDiamond.toFixed(2));
                    $(".redPayD").html((red - coin).toFixed(2));
                    $(".coinPayD").html(coin.toFixed(2));
                    $(".cashPay").html("0.00");
                    $(".wxPay").html("0.00");
                }
                if (red > parseFloat(localStorage.totalPrice) && coin > 0 && coin < totalCash) {
                    $(this).attr("payType", 2);
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(".diamondPay").html(totalDiamond.toFixed(2));
                    $(".coinPayD").html(coin.toFixed(2));
                    $(".redPayD").html((totalCash - coin).toFixed(2));
                }
                if (coin > parseFloat(localStorage.totalPrice) && red > 0 && red < totalCash) {
                    $(this).attr("payType", 2);
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(".diamondPay").html(totalDiamond.toFixed(2));
                    $(".coinPayD").html(totalCash.toFixed(2));
                    $(".redPayD").html("0.00");
                }
                if (coin > totalCash && red > totalCash) {
                    $(this).attr("payType", 2);
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(".diamondPay").html(totalDiamond.toFixed(2));
                    $(".coinPayD").html(totalCash.toFixed(2));
                    $(".redPayD").html("0.00");
                    $(".cashPay").html("0.00");
                    $(".wxPay").html("0.00");
                }
            }
        }
    });
    //使用金币抵扣
    $(".orderDetail").on("click", ".goldBtn", function () {
        var payType = parseInt($(".diamondBtn").attr("payType"));
        var redPayStatus = parseInt($(".redPayBtn").attr("redPayStatus"));
        //现金加钻石支付
        if (payType === 2) {
            if (redPayStatus === 0) {
                // if (totalCash === 0) return false;
                if (parseInt($(this).attr("goldStatus")) === 0) {
                    //使用金币抵扣
                    var coin = parseFloat($(".coinL").html());
                    if (coin === 0) return false;
                    //如果金币大于或等于需要支付的现金
                    if (coin >= totalCash) {
                        $(".cashPay").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".coinPayD").html(totalCash.toFixed(2));
                    } else if (coin < totalCash) {
                        $(".cashPay").html((totalCash - coin).toFixed(2));
                        $(".wxPay").html((totalCash - coin).toFixed(2));
                        $(".coinPayD").html(coin.toFixed(2));
                    }
                    $(".cashPay").html();
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(this).attr("goldStatus", 1);
                } else if (parseInt($(this).attr("goldStatus")) === 1) {
                    //取消金币抵扣
                    $(".cashPay").html(totalCash.toFixed(2));
                    $(".wxPay").html(totalCash.toFixed(2));
                    $(".coinPayD").html("0.00");
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(this).attr("goldStatus", 0);
                }
            } else if (redPayStatus === 1) {
                var red = parseFloat($(".redL").html());
                if (parseInt($(this).attr("goldStatus")) === 0) {
                    //使用金币抵扣
                    var coin = parseFloat($(".coinL").html());
                    if (coin === 0) {
                        return false;
                    }
                    //如果金币大于或等于需要支付的现金
                    if (coin >= totalCash) {
                        $(".cashPay").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".coinPayD").html(totalCash.toFixed(2));
                        $(".redPayD").html("0.00");
                    }
                    else if (coin < (totalCash)) {//-red
                        $(".coinPayD").html(coin.toFixed(2));
                        if (red >= totalCash - coin) {
                            $(".redPayD").html((totalCash - coin).toFixed(2));
                            $(".cashPay").html("0.00");
                            $(".wxPay").html("0.00");
                        } else if (red < totalCash - coin) {
                            $(".redPayD").html(red.toFixed(2));
                            $(".cashPay").html((totalCash - coin - red).toFixed(2));
                            $(".wxPay").html((totalCash - coin - red).toFixed(2));
                        }
                    }
                    $(".cashPay").html();
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(this).attr("goldStatus", 1);
                } else if (parseInt($(this).attr("goldStatus")) === 1) {
                    //取消金币抵扣
                    if (totalCash - red >= 0) {
                        $(".cashPay").html((totalCash - red).toFixed(2));
                        $(".wxPay").html((totalCash - red).toFixed(2));
                        $(".redPayD").html(red.toFixed(2));
                    } else if (totalCash - red < 0) {
                        $(".cashPay").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".redPayD").html(totalCash.toFixed(2));
                    }
                    $(".coinPayD").html("0.00");
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(this).attr("goldStatus", 0);
                }
            }
            //现金支付
        } else if (payType === 1) {
            if (redPayStatus === 0) {
                if (parseInt($(this).attr("goldStatus")) === 0) {
                    //使用金币抵扣
                    var coin = parseFloat($(".coinL").html());
                    if (coin === 0) return false;
                    //如何金币大于或等于需要支付的现金
                    if (coin >= parseFloat(localStorage.totalPrice)) {
                        $(".cashPay").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".coinPayD").html(parseFloat(localStorage.totalPrice).toFixed(2));
                    } else if (coin < parseFloat(localStorage.totalPrice)) {
                        $(".cashPay").html((parseFloat(localStorage.totalPrice) - coin).toFixed(2));
                        $(".wxPay").html((parseFloat(localStorage.totalPrice) - coin).toFixed(2));
                        $(".coinPayD").html(coin.toFixed(2));
                    }
                    $(".cashPay").html();
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(this).attr("goldStatus", 1);
                } else if (parseInt($(this).attr("goldStatus")) === 1) {
                    //取消金币抵扣
                    $(".cashPay").html((parseFloat(localStorage.totalPrice)).toFixed(2));
                    $(".wxPay").html((parseFloat(localStorage.totalPrice)).toFixed(2));
                    $(".coinPayD").html("0.00");
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(this).attr("goldStatus", 0);
                }
            } else if (redPayStatus === 1) {
                var red = parseFloat($(".redL").html());
                if (parseInt($(this).attr("goldStatus")) === 0) {
                    //使用金币抵扣
                    var coin = parseFloat($(".coinL").html());
                    if (coin === 0) return false;
                    if (coin >= parseFloat(localStorage.totalPrice)) {
                        $(".coinPayD").html(parseFloat(localStorage.totalPrice).toFixed(2));
                        $(".wxPay").html("0.00");
                        $(".cashPay").html("0.00");
                        $(".redPayD").html("0.00");
                    } else if (coin < parseFloat(localStorage.totalPrice)) {
                        $(".coinPayD").html(coin.toFixed(2));
                        if (red > parseFloat(localStorage.totalPrice) - coin) {
                            $(".wxPay").html("0.00");
                            $(".cashPay").html("0.00");
                            $(".redPayD").html((parseFloat(localStorage.totalPrice) - coin).toFixed(2));
                        } else if (red < parseFloat(localStorage.totalPrice) - coin) {
                            $(".wxPay").html((parseFloat(localStorage.totalPrice) - coin - red).toFixed(2));
                            $(".cashPay").html((parseFloat(localStorage.totalPrice) - coin - red).toFixed(2));
                            $(".redPayD").html(red.toFixed(2));
                        }
                    }
                    $(".cashPay").html();
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(this).attr("goldStatus", 1);
                } else if (parseInt($(this).attr("goldStatus")) === 1) {
                    //取消金币抵扣 
                    if (parseFloat(localStorage.totalPrice) - red > 0) {
                        $(".cashPay").html((parseFloat(localStorage.totalPrice) - red).toFixed(2));
                        $(".wxPay").html((parseFloat(localStorage.totalPrice) - red).toFixed(2));
                        $(".redPayD").html(red.toFixed(2));
                    } else if (parseFloat(localStorage.totalPrice) - red <= 0) {
                        $(".cashPay").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".redPayD").html(localStorage.totalPrice);
                    }
                    $(".coinPayD").html("0.00");
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(this).attr("goldStatus", 0);
                }
            }
        }
    });
    //使用红包抵扣
    $(".orderDetail").on("click", ".redPayBtn", function () {
        var payType = parseInt($(".diamondBtn").attr("payType"));
        var goldStatus = parseInt($(".goldBtn").attr("goldStatus"));
        //现金加钻石支付
        if (payType === 2) {
            if (goldStatus === 0) {
                if (parseInt($(this).attr("redPayStatus")) === 0) {
                    //使用金币抵扣
                    var red = parseFloat($(".redL").html());
                    if (red === 0) return false;
                    //如果金币大于或等于需要支付的现金
                    if (red >= totalCash) {
                        $(".cashPay").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".redPayD").html(totalCash.toFixed(2));
                    } else if (red < totalCash) {
                        $(".cashPay").html((totalCash - red).toFixed(2));
                        $(".wxPay").html((totalCash - red).toFixed(2));
                        $(".redPayD").html(red.toFixed(2));
                    }
                    $(".cashPay").html();
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(this).attr("redPayStatus", 1);
                } else if (parseInt($(this).attr("redPayStatus")) === 1) {
                    //取消金币抵扣
                    $(".cashPay").html(totalCash.toFixed(2));
                    $(".wxPay").html(totalCash.toFixed(2));
                    $(".redPayD").html("0.00");
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(this).attr("redPayStatus", 0);
                }
            } else if (goldStatus === 1) {
                var coin = parseFloat($(".coinL").html());
                if (parseInt($(this).attr("redPayStatus")) === 0) {
                    //使用金币抵扣
                    var red = parseFloat($(".redL").html());
                    if (red === 0) return false;
                    //如果金币大于或等于需要支付的现金
                    if (coin - totalCash >= 0) {
                        $(".cashPay").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".redPayD").html("0.00");
                    } else if (coin - totalCash < 0) {
                        if (red >= totalCash - coin) {
                            $(".cashPay").html("0.00");
                            $(".wxPay").html("0.00");
                            $(".redPayD").html((totalCash - coin).toFixed(2));
                        } else if (red < totalCash - coin) {
                            $(".cashPay").html((totalCash - coin - red).toFixed(2));
                            $(".wxPay").html((totalCash - coin - red).toFixed(2));
                            $(".redPayD").html(red.toFixed(2));
                        }
                    }
                    $(".cashPay").html();
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(this).attr("redPayStatus", 1);
                } else if (parseInt($(this).attr("redPayStatus")) === 1) {
                    //取消金币抵扣
                    if (totalCash - coin > 0) {
                        $(".cashPay").html((totalCash - coin).toFixed(2));
                        $(".wxPay").html((totalCash - coin).toFixed(2));
                    } else if (totalCash - coin < 0) {
                        $(".cashPay").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".coinPayD").html(totalCash.toFixed(2));
                    }
                    $(".redPayD").html("0.00");
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(this).attr("redPayStatus", 0);
                }
            }
            //现金支付
        } else if (payType === 1) {
            if (goldStatus === 0) {
                if (parseInt($(this).attr("redPayStatus")) === 0) {
                    //使用金币抵扣
                    var red = parseFloat($(".redL").html());
                    if (red === 0) return false;
                    //如何金币大于或等于需要支付的现金
                    if (red >= parseFloat(localStorage.totalPrice)) {
                        $(".cashPay").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".redPayD").html(parseFloat(localStorage.totalPrice).toFixed(2));
                    } else if (red < parseFloat(localStorage.totalPrice)) {
                        $(".cashPay").html((parseFloat(localStorage.totalPrice) - red).toFixed(2));
                        $(".wxPay").html((parseFloat(localStorage.totalPrice) - red).toFixed(2));
                        $(".redPayD").html(red.toFixed(2));
                    }
                    $(".cashPay").html();
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(this).attr("redPayStatus", 1);
                } else if (parseInt($(this).attr("redPayStatus")) === 1) {
                    //取消金币抵扣
                    $(".cashPay").html((parseFloat(localStorage.totalPrice)).toFixed(2));
                    $(".wxPay").html((parseFloat(localStorage.totalPrice)).toFixed(2));
                    $(".redPayD").html("0.00");
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(this).attr("redPayStatus", 0);
                }
            } else if (goldStatus === 1) {
                var coin = parseFloat($(".coinL").html());
                if (coin >= parseFloat(localStorage.totalPrice) && parseInt($(".redPayBtn").attr("redPayStatus")) === 0) {
                    return false;
                }
                if (parseInt($(this).attr("redPayStatus")) === 0) {
                    var red = parseFloat($(".redL").html());
                    if (red === 0) return false;
                    if (red >= (parseFloat(localStorage.totalPrice) - coin)) {
                        $(".cashPay").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".redPayD").html((parseFloat(localStorage.totalPrice) - coin).toFixed(2));
                    } else if (red < (parseFloat(localStorage.totalPrice) - coin)) {
                        $(".cashPay").html((parseFloat(localStorage.totalPrice) - coin - red).toFixed(2));
                        $(".wxPay").html((parseFloat(localStorage.totalPrice) - coin - red).toFixed(2));
                        $(".redPayD").html(red.toFixed(2));
                    }
                    $(".cashPay").html();
                    $(this).find("img").attr("src", "../../static/img/btn_off@3x.png");
                    $(this).attr("redPayStatus", 1);
                } else if (parseInt($(this).attr("redPayStatus")) === 1) {
                    if (parseFloat(localStorage.totalPrice) - coin > 0) {
                        $(".cashPay").html((parseFloat(localStorage.totalPrice) - coin).toFixed(2));
                        $(".wxPay").html((parseFloat(localStorage.totalPrice) - coin).toFixed(2));
                    } else if (parseFloat(localStorage.totalPrice) - coin < 0) {
                        $(".cashPay").html("0.00");
                        $(".wxPay").html("0.00");
                        $(".coinPayD").html(localStorage.totalPrice);
                    }
                    $(".redPayD").html("0.00");
                    $(this).find("img").attr("src", "../../static/img/btn_UN@3x.png");
                    $(this).attr("redPayStatus", 0);
                }
            }
        }
    });
    //历史回退
    $(".backArrow").on("click", function () {
        history.back();
    });
    //选择地址
    $(".orderDetail").on("click", ".address", function () {
        localStorage.choseAddressStatus = 1;
        location.href = "./address.html";
    });
    //提交订单
    function tips(tips) {
        $(".tips").css("display", "block").html(tips).animate({
            "opacity": 1
        }, 1000, function () {
            setTimeout(function () {
                $(".tips").animate({
                    "opacity": 0
                }, 1000, function () {
                    $(".tips").css("display", "none");
                })
            }, 1000);
        })
    }
    var flag = false;
    $(".orderDetail").on("click", ".right", function () {
        if(flag) return false;
        var payType = parseInt($(".diamondBtn").attr("payType"));
        var hongbao = parseInt($(".redPayBtn").attr("redPayStatus"));
        var gold = parseInt($(".goldBtn").attr("goldStatus"));
        var addressId = $(".address").attr("data-addressId");
        var message = $(".message").val();
        $.ajax({
            type: "post",
            url: domain.test + "/submitorder",
            // url: domain.online + "/cart/create",
            data: {
                data: data,
                payType: payType,
                hongbao: hongbao,
                gold: gold,
                addressId: addressId,
                message: message
            },
            beforeSend: function (xhr) {
                flag = true;
                $(".loadingMarsk").css("background-color","rgba(0,0,0,.3)");
                $(".loadingMarsk").css("display","block");
                xhr.setRequestHeader("token", token);
                xhr.setRequestHeader("version", "iOS2.91");
            },
            success: function (response) {
                console.log(response);
                if (response.data.rtnCode === 200) {
                    // if (response.rtnCode === 200) {
                    var orderId = response.data.data.orderId;
                    // var orderId = response.data.orderId;
                    $(".loadingMarsk").css("display","none");
                    $.ajax({
                        type: "post",
                        url: domain.test + "/wxpay",
                        // url: domain.online + "/wxpay/payBefore",
                        data: { orderId: orderId },
                        beforeSend: function (xhr) {
                            xhr.setRequestHeader("token", token);
                            xhr.setRequestHeader("version", "iOS2.91");
                        },
                        success: function (response) {
                            console.log(response);
                            if(response.data.rtnCode===200) {
                                var payMsg = response.data.data;
                                console.log(payMsg);
                                function onBridgeReady(){
                                    WeixinJSBridge.invoke(
                                        'getBrandWCPayRequest', {
                                            "appId":"wx2421b1c4370ec43b",     //公众号名称，由商户传入     
                                            "timeStamp":"1395712654",         //时间戳，自1970年以来的秒数     
                                            "nonceStr":"e61463f8efa94090b1f366cccfbbb444", //随机串     
                                            "package":"prepay_id=u802345jgfjsdfgsdg888",     
                                            "signType":"MD5",         //微信签名方式：     
                                            "paySign":"70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 
                                        },
                                        function(res){     
                                            if(res.err_msg == "get_brand_wcpay_request:ok" ) {}     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。 
                                        }
                                    ); 
                                 }
                                 if (typeof WeixinJSBridge == "undefined"){
                                    if( document.addEventListener ){
                                        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                                    }else if (document.attachEvent){
                                        document.attachEvent('WeixinJSBridgeReady', onBridgeReady); 
                                        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                                    }
                                 }else{
                                    onBridgeReady();
                                 }
                            } 
                        }
                    });
                    flag = false;
                }
                if (response.data.rtnCode === 400) {
                    $(".loadingMarsk").css("display","none");
                    tips(response.data.msg);
                    flag = false;
                }
                if (response.data.rtnCode === 401) {
                    // if (response.rtnCode === 401) {
                    var outTimeCode = response.data.rtnCode;
                    // var outTimeCode = response.rtnCode;
                    location.href = "../../index.html?outTimeCode=" + outTimeCode;
                }
            }
        });
    });
});